package com.orientation.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orientation.commom.Result;
import com.orientation.entity.Goods;
import com.orientation.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 物品管理表 前端控制器
 * </p>
 *
 * @author xuandian
 * @since 2022-01-09
 */
@Api(tags = "物资管理")
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Resource
    private GoodsService goodsService;

    @ApiOperation("添加")
    @PostMapping("/insertGoods")
    public ResponseEntity<Result> insertGoods(@RequestParam(value = "files", required = false) MultipartFile[] files,
                                              @RequestParam(value = "goodsName", required = true) String goodsName,
                                              @RequestParam(value = "goodsType", required = false) String goodsType) {
        goodsService.insertGoods(files, goodsName, goodsType);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("修改")
    @PostMapping("/updateGoods")
    public ResponseEntity<Result> updateGoods(@RequestParam(value = "files", required = false) MultipartFile[] files,
                                              @RequestParam(value = "sid", required = true) String sid,
                                              @RequestParam(value = "urls", required = false) List<String> urls,
                                              @RequestParam(value = "goodsName", required = false) String goodsName,
                                              @RequestParam(value = "goodsType", required = false) String goodsType) {
        goodsService.updateGoods(files, sid, urls, goodsName, goodsType);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable String id) {
        return goodsService.removeById(id);
    }

    @ApiOperation("查询单条")
    @GetMapping("/{id}")
    public Goods findById(@PathVariable String id) {
        return goodsService.getById(id);
    }

    @ApiOperation("根据学号进行物资可选列表")
    @GetMapping("/queryGoods/{studentId}")
    public ResponseEntity<Result> queryGoods(@PathVariable String studentId) {
        List<Goods> goodsList = goodsService.queryGoods(studentId);
        return ResponseEntity.ok(Result.success(goodsList));
    }

    @ApiOperation("查询所有")
    @GetMapping("/getAll")
    public List<Goods> findAll() {
        return goodsService.list();
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public IPage<Goods> findPage(
            @RequestParam(value = "goodsName", required = false) String goodsName,
            @RequestParam(value = "goodsType", required = false) String goodsType,
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return goodsService.page(new Page<>(pageNum, pageSize), Wrappers.<Goods>lambdaQuery().
                like(StringUtils.isNotBlank(goodsName), Goods::getGoodsName, goodsName)
                .like(StringUtils.isNotBlank(goodsType), Goods::getGoodsType, goodsType));
    }

}

